﻿/**
  ******************************************************************************
  * File:    1wire.c
  * Author:  Michal Mazur
  * Date:    11-October-2013 -Last modification
  * Brief:   This file contains definitions for 1wire interface
  ******************************************************************************
  * Attention
  *
  *
  *             Copyright (c) 2013 by HC . All rights reserved
  ******************************************************************************
  *****************************************************************************/

// Includes ********************************************************************
#include "one_wire.h"
// Functions *******************************************************************

/**
  * Brief: This function received key from keyboard.
  * Parameters:   None
  * Return value: Received key
  */
void OneWire_Init(void)
{
	ONE_WIRE_PIN_IN;
}/*
 ***** End Get_Kay function ****************************************************
 -------------------------------------------------------------------------------
 */


/**
  * Brief: This function .
  * Parameters:   None
  * Return value:
  */
uint8_t OneWire_Reset(void)
{
	uint8_t data=0;

	ONE_WIRE_PIN_OUT;
	ONE_WIRE_PIN_RESET;
	_delay_us(500);
	ONE_WIRE_PIN_IN;
	_delay_us(50);
	data = ONE_WIRE_PIN_READ;
	_delay_us(450);
	return data;

}/*
 ***** End Get_Kay function ****************************************************
 -------------------------------------------------------------------------------
 */

/**
  * Brief: This function .
  * Parameters:   None
  * Return value:
  */
void OneWire_Send(uint8_t bit)
{
	ONE_WIRE_PIN_OUT;
	ONE_WIRE_PIN_RESET;
	_delay_us(5);
	if(bit == 1) ONE_WIRE_PIN_IN;
	_delay_us(80);
	ONE_WIRE_PIN_IN;
	_delay_us(5);

}/*
 ***** End Get_Kay function ****************************************************
 -------------------------------------------------------------------------------
 */

/**
  * Brief: This function .
  * Parameters:   None
  * Return value:
  */
uint8_t OneWire_Read(void)
{
	uint8_t data=0;

	ONE_WIRE_PIN_OUT;
	ONE_WIRE_PIN_RESET;
	_delay_us(2);
	ONE_WIRE_PIN_IN;
	_delay_us(10);
	if(ONE_WIRE_PIN_READ) data=1;
	_delay_us(55);
	return data;
}/*
 ***** End Get_Kay function ****************************************************
 -------------------------------------------------------------------------------
 */


/**
  * Brief: This function .
  * Parameters:   None
  * Return value:
  */
void OneWire_SendByte(uint8_t data)
{
	uint8_t num_byte = 8;

	while(num_byte)
	{
		OneWire_Send(data & 0x01);
		data >>= 1;
		num_byte--;
	}
}/*
 ***** End Get_Kay function ****************************************************
 -------------------------------------------------------------------------------
 */

/**
  * Brief: This function .
  * Parameters:   None
  * Return value:
  */
uint8_t OneWire_ReadByte(void)
{
	uint8_t data=0;
	uint8_t num_byte = 0;

	for(num_byte=0; num_byte<8; num_byte++)
	{
		data |= (OneWire_Read() << num_byte);
	}
	return data;
}/*
 ***** End Get_Kay function ****************************************************
 -------------------------------------------------------------------------------
 */
/* END OF FILE *****************************************************************
 *
 */
